//version: 1.0.3
package roster;

import java.util.Scanner;
import java.lang.String;

public class IO {
	private static Schedule currentSchedule;
	private static boolean previousLoad=false;
	private static boolean previousSave=false;
	private static Scanner reader = new Scanner(System.in);
	
	private void loadSchedule(String name){
		if(Controller.readSchedule(name)){
			currentSchedule=Controller.getReadScheduleResult();
			System.out.println("\nOperated Seccessfully!");
			previousLoad=true;
		}else{
			System.out.println("\nUnable to operate the schedule.");
			previousLoad=false;
		}
	}
	
	private void saveSchedule(){
		if(Controller.writeSchedule(currentSchedule)){
			System.out.println("\nSaved Successfully!");
			previousSave=true;
		}else{
			System.out.println("\nUnable to save the schedule.");
			previousSave=false;
		}
	}
	
	public void chooseSchedule(String MsgStr){
		String[] list=Controller.getAllScheduleNames();
		if(list.length==0){
			System.out.println("\nNo existing schedule. ");
			return;
		}
		int input;
		do{
			System.out.println("");
			for(int i=0;i<list.length;i++){
				System.out.println((i+1)+". "+list[i]);
			}
			System.out.print("\nPlease choose a schedule to " + MsgStr + ": ");
			input=reader.nextInt();
			
			if(input>list.length || input<=0)
				System.out.println("\nInvalid Input!");
			else
				loadSchedule(list[input-1]);
		}while(input>list.length || input<=0);
		if(reader.hasNextLine())reader.nextLine();
	}
	
	public void viewSchedule(){
		chooseSchedule("VIEW");
		if(previousLoad)
			System.out.println("\n"+currentSchedule.toString());
	}
	
	public void createSchedule(){
		System.out.print("\nPlease enter a new schedule name: ");
		String name=reader.nextLine();
		System.out.print("Please enter a start date(DDMMYYYY): ");
		int date=reader.nextInt();
		for(int i=0;i<Controller.getAllScheduleNames().length;i++){
			if(Controller.getAllScheduleNames()[i].equalsIgnoreCase(name)){
				System.out.println("\nError: Schedule \"" + Controller.getAllScheduleNames()[i] + "\" already exists.");
				return;
			}
		}
		currentSchedule=new Schedule(name,date);
		saveSchedule();
		if(previousSave)
			System.out.println("Please edit it in \"3. Modify an existing schedule\".");
	}
	
	public void deleteSchedule(){
		chooseSchedule("DELETE");
		if(Controller.deleteSchedule(currentSchedule))
			System.out.println("The schedule \""+currentSchedule.getName()+"\" was DELETED.");
	}
	
	public void modifySchedule(){
		chooseSchedule("MODIFY");
		String input="";
		do{
			if(!input.equals("") && !input.equals("help")){
				if(ScheduleModifier.query(currentSchedule, input))
					currentSchedule=ScheduleModifier.getResult();
				else
					System.out.println("\nInvalid Command.");
			}
			System.out.println("\n"+currentSchedule.toString());
			
			if(input.equals("help")){
				String str="Follow the formats below.\n" +
						   "To change date  :   change date DDMMYYYY\n" +
						   "To change period:   change i j VALUE\n" +
						   "To add staff    :   add STAFFNAME\n" +
						   "To delete staff :   delete i\n";
				System.out.println("\n"+str);
			}
			
			System.out.print("\nInput a command(\"help\" for explanation, \"save\" to quit): ");
			input=reader.nextLine();
			
		}while(!input.equals("save"));
		saveSchedule();
	}
	
	public void ioProcess()
	{
		System.out.println("Weekly Roster (ver 2.0)	Designed by WeLove3343\n");
		System.out.print("User ID: ");
		String id = reader.nextLine();
		System.out.print("Password: ");
		String pw = reader.nextLine();
		System.out.println();
		System.out.println("Successfully Login!");
		
		int input;
		do
		{			
			System.out.println();
			System.out.println("1. Create a new schedule");
			System.out.println("2. Delete an existing schedule");
			System.out.println("3. Modify an existing schedule");
			System.out.println("4. View an existing schedule");
			System.out.println("0. Quit");
			
			System.out.print("\nPlease choose a function: ");
			
			
			input=(reader.hasNextInt())?reader.nextInt():-1;
			if(reader.hasNextLine())reader.nextLine();
			
			switch(input){
				case 0: System.out.println("\nProgram ends.");
						break;
				case 1: createSchedule();
						break;
				case 2: deleteSchedule();
						break;
				case 3: modifySchedule();
						break;
				case 4: viewSchedule();
						break;
				default: System.out.println("\nInvalid Input!");
			}
		}while(input!=0);		
	}	
}